package work.soho.proxy.ui;

import work.soho.proxy.PartsConfigure;
import work.soho.proxy.ProxyApplication;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class ConfigureFrame {
    private static ConfigureFrame configureFrame;

    /**
     * 配置窗口
     */
    private JFrame jFrame;

    /**
     * 单例获取
     *
     * @return
     */
    public static ConfigureFrame instance() {
        if(configureFrame == null) {
            configureFrame = new ConfigureFrame();
            configureFrame.create();
        }
        return configureFrame;
    }

    private ConfigureFrame() {

    }

    /**
     * 从配置文件读取配置信息
     *
     * @param key
     * @return
     */
    private String getValue(String key) {
        try {
            String str = ProxyApplication.configManage.getValue(key);
            if(str != null) {
                return str;
            }
        } catch (Exception e) {
            //ignore
        }
        return "";
    }



    public void show() {
        jFrame.setVisible(true);
    }

    private void create() {
        jFrame = new JFrame("代理配置");
        URL uri = Thread.currentThread().getContextClassLoader().getResource("images/yellow-proxy-icon.png");
        jFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(uri));
        jFrame.setSize(600, 1200);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        GridLayout glo = new GridLayout(6, 2);
        jFrame.setLayout(glo);

        JPanel panel = new JPanel();

        //key id
        final JLabel idLabel = new JLabel();
        final JTextField idField = new JTextField(20);
        idLabel.setText("阿里云 accessKeyId");
        idLabel.setLabelFor(idField);
        panel.add(idLabel);
        idField.setFont(new Font(null, Font.PLAIN, 20));
        idField.setText(getValue("accessKeyId"));
        panel.add(idField);

        //秘钥
        final JLabel keyLabel = new JLabel();
        final JTextField keyField = new JTextField(20);
        keyLabel.setLabelFor(keyField);
        keyLabel.setText("阿里云 accessSecret");
        panel.add(keyLabel);
        keyField.setFont(new Font(null, Font.PLAIN, 20));
        keyField.setText(getValue("accessSecret"));
        panel.add(keyField);

        //秘钥名
        final JLabel keyPairNameLabel = new JLabel();
        final JTextField keyPairNameField = new JTextField(20);
        keyPairNameLabel.setLabelFor(keyPairNameField);
        keyPairNameLabel.setText("阿里云秘钥名");
        panel.add(keyPairNameLabel);
        keyPairNameField.setFont(new Font(null, Font.PLAIN, 20));
        String keyPairName = getValue("keyPairName");
        PartsConfigure partsConfigure = new PartsConfigure();
        keyPairNameField.setText("".equals(keyPairName) ? partsConfigure.getKeyPairName() : keyPairName);
        panel.add(keyPairNameField);

        //是否启用socket5代理
        final JLabel serviceLabel = new JLabel();
        serviceLabel.setText("启用服务选择：");
        panel.add(serviceLabel);
        final JCheckBox useSocket5 = new JCheckBox("是否启用socket5", null, ProxyApplication.configManage.getBoolValue("useSocket5"));
        panel.add(useSocket5);
        //是否启用socket5代理
        final JCheckBox useDomain = new JCheckBox("是否启用DDNS", null, ProxyApplication.configManage.getBoolValue("useDdns"));
        panel.add(useDomain);

        //代理端口
        final JTextField proxPortField = new JTextField(20);
        final JLabel proxPortLabel = new JLabel();
        proxPortLabel.setLabelFor(proxPortField);
        proxPortLabel.setText("本地代理端口");
        panel.add(proxPortLabel);
        proxPortField.setFont(new Font(null, Font.PLAIN, 20));
        proxPortField.setText(getValue("port"));
        panel.add(proxPortField);

        //自动检查链接状况
        final JTextField autoCheckTsField = new JTextField(20);
        final JLabel autoChecktsLabel = new JLabel();
        autoChecktsLabel.setLabelFor(autoCheckTsField);
        autoChecktsLabel.setText("自动检查时间(单位秒，<=0则不检查)");
        panel.add(autoChecktsLabel);
        autoCheckTsField.setFont(new Font(null, Font.PLAIN, 20));
        autoCheckTsField.setText(getValue("autoCheckTs"));
        panel.add(autoCheckTsField);

        //配置自动解析域名基本域名
        final JTextField baseDomainFiled = new JTextField(20);
        final JLabel baseDomainLabel = new JLabel();
        baseDomainLabel.setLabelFor(baseDomainFiled);
        baseDomainLabel.setText("DDNS域名（内网前缀为lan,广域网为internet）");
        panel.add(baseDomainLabel);
        baseDomainFiled.setFont(new Font(null, Font.PLAIN, 20));
        baseDomainFiled.setText(getValue("baseDomain"));
        panel.add(baseDomainFiled);

        JButton okButton = new JButton("保 存");
        okButton.setFont(new Font(null, Font.PLAIN, 20));
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = idField.getText();
                String key = keyField.getText();
                String keyPairName = keyPairNameField.getText();
                String proxyPort = proxPortField.getText();
                System.out.println(id + key + proxyPort);
                PartsConfigure partsConfigure = new PartsConfigure();
                partsConfigure.setAccessKeyId(id);
                partsConfigure.setAccessSecret(key);
                partsConfigure.setKeyPairName(keyPairName);
                partsConfigure.setProxyPort(proxyPort);
                partsConfigure.setBaseDomain(baseDomainFiled.getText());
                partsConfigure.setAutoCheckTs(new Integer(autoCheckTsField.getText()==""? "0" :autoCheckTsField.getText()));
                partsConfigure.setUseDdns(useDomain.isSelected());
                partsConfigure.setUseSocket5(useSocket5.isSelected());
                try {
                    ProxyApplication.configManage.save2File(partsConfigure);
                    ProxyApplication.configManage.reloadPartsConfigure();
                    ProxyApplication.infoBox("配置文件保存成功", "阿里代理");
                    synchronized (jFrame) {
                        jFrame.notifyAll();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                    ProxyApplication.infoBox("用户目录下的配置文件： proxy.ini 存储失败， 请确保拥有写入权限", "错误提示");
                }
                jFrame.dispose();
            }
        });
        panel.add(okButton);
        jFrame.setBounds(300, 200, 400, 500);
        jFrame.setContentPane(panel);
        jFrame.setVisible(false);
    }

    /**
     * 获取界面
     *
     * @return
     */
    public JFrame getFrame() {
        return jFrame;
    }
}
